package com.cinsea.ext.cjspx;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.common.Page;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.log.service.LogService;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: xf
 * Date: 12-9-25
 * Time: 上午9:00
 */
public class TopicAction extends ExtAction {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        HumanService humanService = Env.getBean("humanService");
        if (action.equalsIgnoreCase("gettopics")) {
            LogService logService = Env.getBean("logService");
            String dowid = StringUtil.null2String(request.getParameter("dowid"));
            String types = StringUtil.null2String(request.getParameter("types"));
            int pageNo = NumberUtil.string2Int(request.getParameter("pageno"), 1);
            int pageSize = NumberUtil.string2Int(request.getParameter("pagesize"), 20);
            if (!StringUtil.isEmpty(request.getParameter("start")))
                pageNo = NumberUtil.string2Int(request.getParameter("start"), 0) / pageSize + 1;
            String topicsql="select id,topic,createdatetime,createuserid from cs_topic where deleted!=1 and  dowid = ? and types=? order by modifydatetime desc,createdatetime desc";
            topicsql= JdbcUtil.getPermissionSql(topicsql, "cs_topic");
            Page pageObject = jdbcDao.pagedQuery(topicsql, new Object[]{dowid, types}, pageNo, pageSize);
            List array = new ArrayList();
            if (pageObject.getTotalSize() != 0) {
                List list = (List) pageObject.getResult();
                for (int i = 0; i < list.size(); i++) {
                    Map object = new HashMap();
                    Map topic = (Map) list.get(i);
                    String objname = "<span>" + topic.get("topic") + "</span>";
                    object.put("topic", objname);
                    object.put("processid", topic.get("id"));
                    List<Map> replys=jdbcDao.queryForList("select processid,createuserid,createdatetime from  cs_topicreply where processid=? order by CREATEDATETIME desc",new Object[]{topic.get("id")});
                    object.put("replycount", replys.size());
                    object.put("replytime", "无");
                    object.put("replyuser", "无");
                    if(replys.size()>0){
                        Map lastreply=replys.get(0);
                        object.put("replytime", lastreply.get("createdatetime"));
                        object.put("replyuser", humanService.getFree((String) lastreply.get("createuserid")).getObjname());
                    }
                    object.put("createtime", topic.get("createdatetime"));
                    object.put("creator", humanService.getFree((String) topic.get("createuserid")).getObjname());
                    if (!logService.hasLog((String) topic.get("id"), Env.getRemoteUser().getId()))
                        object.put("isnew", "1");
                    array.add(object);
                }
            }

            Map objectresult = new HashMap();
            objectresult.put("result", array);
            objectresult.put("totalcount", pageObject.getTotalSize());
            response.getWriter().print(JSON.toJSONString(objectresult));
            return;
        }
    }
}
